import * as CryptoJS from "crypto-js";
import citys from "@/uni_modules/vk-uview-ui/libs/address/citys.json";
// import {useAppStore} from '@/stores/app';
// const appStore = useAppStore();
class Util{
    constructor(){

    }
    checkLogin(){
        let accessToken = uni.getStorageSync('accessToken0');
        if(!accessToken){
          this.gotoLogin("请先登录");
          return false;
        }
        return true;
    };
    gotoLogin(tips?:string) {
      uni.showModal({
        title: '提示',
        content: tips,
        success: res => {
            if (res.confirm) {
              uni.clearStorageSync();
              let pages = getCurrentPages();
              uni.removeStorageSync('accessToken');
              uni.setStorageSync('beforeLogin', pages[pages.length - 1].$page.fullPath);
              uni.navigateTo({
                url: '/pages/login/index'
              });
            //   appStore.showMsgBox = false;
            } else if (res.cancel) {
              uni.switchTab({
                url: '/pages/home/index'
              });
            //   appStore.showMsgBox = false;
            }
        }
      });      
    };
    //路由跳转
    gotoPage(url, param){
        let new_url = url;
        if(param){
          new_url += this.serialize(param);
        }
        uni.navigateTo({
           url: new_url
        })
    };
    //因小程序的请求会将null传过去，在请求传参时，将值为null或''的参数过滤掉
    getParam(data){
        let search_param = {};
        for(let key in data){
          if(data[key] != null && String(data[key]).trim() != ''){
            search_param[key] = data[key];
          }
        }
        return search_param;
    };
    // 根据id值，在列表中找出对应的项，返回名字
    setName(value, list, idKey, nameKey){
        let id = idKey || 'id';
        let name = nameKey || 'name';
        let res = '未知';
        let arr = list.filter(item => item[id] == value);
        if(arr.length == 1){
            res = arr[0][name];
        }
        return res;          
    };
    //重置对象
    resetForm(form, reset_form){
        let obj = JSON.parse(JSON.stringify(reset_form));
        for(var key in form){
            if(reset_form.hasOwnProperty(key)){
                form[key] = obj[key];
            };
        }
    };
    //对象合并
    assignObj(){

    };  
      //数组格式转化
    transformArray(arr, idKey, nameKey){
        let id = idKey || 'value';
        let name = nameKey || 'label';
        let data = [];
        if(arr.length  > 0){
            data = arr.map(item => {return {id: item[id], name: item[name], text: item[name]}});
        }else{
            data = []
        }
        return data;
    };
    // 时间格式兼容性转换
    changeDateFromat(data){
        let new_date = data.replace(/-/g,"/");
        let time = new Date(new_date).getTime();
        return time;
      };
  
      // 序列化转为对象
      format(str){
          let arr = str.split('&');
          let obj = {};
          arr.forEach(item => {
              let item_arr = item.split('=')
              obj[item_arr[0]] = item_arr[1];
          });
          return obj;
      };
    // 对象转为序列化
    serialize(obj){
        let str = '?';
        for(let key in obj){
            str += key + '=' + obj[key] + '&';
        };
        return str.slice(0, str.length-1);
    };
    setCurrentPages(){
        const page = getCurrentPages();
        const currentPage = page[page.length - 1];
        return currentPage || {};
    };
    getWeekName(num, type){
      let name = '';
      let title = '周';
      if(type == 'xq'){
        title = '星期'
      }
      switch(num){
        case 0:
          name = '日';
          break;
        case 1:
          name = '一';
          break;
        case 2:
          name = '二';
          break;
        case 3:
          name = '三';
        break;
        case 4:
          name = '四';
          break;
        case 5:
          name = '五';
          break;
        case 6:
          name = '六';
          break;
      }
      return title + name;
    };
    getCityName(code){
      let arr = [];
      citys.forEach(item => {
        arr = arr.concat(item);
      });
      let name = this.setName(code, arr, 'code', 'name');
      return name;
    };
    //加密处理
    encryption(src: string, keyWord: string) {
        const key = CryptoJS.enc.Utf8.parse(keyWord);
        // 加密
        const encrypted = CryptoJS.AES.encrypt(src, key, {
          iv: key,
          mode: CryptoJS.mode.CFB,
          padding: CryptoJS.pad.NoPadding,
        });
        return encrypted.toString();
    };
    // 加密处理
    encryptionBase64(rawStr: string) {
        let wordArray = CryptoJS.enc.Utf8.parse(rawStr);
        return CryptoJS.enc.Base64.stringify(wordArray);
      }
      
}

export default new Util();